package com.yimixian.app.ui.dialog;

import com.yimixian.app.R;
import com.yimixian.app.adapter.CartDialogRecyclerAdapter;
import com.yimixian.app.cart.CartManager;
import com.yimixian.app.model.CartUpdate;
import com.yimixian.app.model.KV;
import com.ymx.sdk.util.DeviceUtils;

import android.app.Activity;
import android.app.Dialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

/**
 * 轮子弹出框
 *
 * @author liuletao
 */
public class CartDialog extends Dialog implements View.OnClickListener {

    public interface ClickSureListener {
        void onConfirm(KV kv);
        void onTwoSelectConfirm(String province, String city);
    }

    private ClickSureListener mClickSureListener;

    Activity activity;
    Button cartCancel;
    RelativeLayout lin_parent;
    RecyclerView mRecyclerView;
    CartDialogRecyclerAdapter mAdapter;
    CartUpdate mCartUpdate;

    public CartDialog(Activity activity, CartUpdate mCartUpdate) {
        super(activity, R.style.DialogStyle);
        this.activity = activity;
        this.mCartUpdate = mCartUpdate;
        initView();
    }

    private void initView() {
        setContentView(R.layout.dialog_cart);
        lin_parent = (RelativeLayout) findViewById(R.id.lin_parent);
        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = getScreenWidth();
        mWindow.setGravity(Gravity.BOTTOM);
        // 添加动画
        mWindow.setWindowAnimations(R.style.dialogAnim);
        mWindow.setAttributes(lp);
        if (CartManager.getInstance().mCartUpdate == null) return;
        mAdapter = new CartDialogRecyclerAdapter(activity,
                CartManager.getInstance().mCartUpdate.delieverGoodsGroup, this);

        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(activity));
        mRecyclerView.setAdapter(mAdapter);
        lin_parent.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                DeviceUtils.getScreenHeight(activity) * 3 / 5));
//        DividerLine dividerLine = new DividerLine(DividerLine.VERTICAL);
//        dividerLine.setSize(DensityUtils.dp2px(getContext(), 5));
//        dividerLine.setColor(R.color.no_wight_bg);

//        mRecyclerView.addItemDecoration(dividerLine);
        cartCancel = (Button) findViewById(R.id.cart_cancel);
        cartCancel.setOnClickListener(this);
    }

    /** 获取屏幕分辨率宽 */
    public int getScreenWidth() {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cart_cancel:
                dismiss();
                break;
            default:
                break;
        }
    }

    public void setmClickSureListener(ClickSureListener mClickSureListener) {
        this.mClickSureListener = mClickSureListener;
    }

}
